using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ALib;
using ALib.Data.CSVOperation;
using Remoting;
using TradingLab.Data;

namespace WinRemoteClient_Alpha
{
    public partial class RemoteClientForm_Alpha : Form
    {
        private static AlphaRemotingObject _remoteObject;

        public RemoteClientForm_Alpha()
        {
            InitializeComponent();
            textBoxIp.Text = GetLocalIp();
            Logger.LogDir = new DirectoryInfo(@"Log\" + DateTime.Now.ToString(DateTimeFormatType.Simple));
        }

        public string GetLocalIp()
        {
            string result = null;

            IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress ipAddress in ipHostEntry.AddressList)
            {
                if (ipAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    result = ipAddress.ToString();
                }
            }
            return result;
        }


        private void buttonTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                if (_remoteObject == null)
                {
                    string url = string.Format(@"tcp://{0}:{1}/Alpha", textBoxIp.Text, textBoxPort.Text);
                    _remoteObject = new AlphaRemotingObject(url);
                    buttonConnect.Enabled = false;
                    buttonRequest.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                textBoxResponse.Text = ex.ToString();
            }
        }

        private void buttonRequest_Click(object sender, EventArgs e)
        {
            try
            {
                Debug.Assert(_remoteObject!=null);

                string response = _remoteObject.Request(textBoxRequest.Text);
                textBoxResponse.Text = response;
            }
            catch (Exception ex)
            {
                textBoxResponse.Text = ex.ToString();
            }
        }

        private void buttonAutoTest_Click(object sender, EventArgs e)
        {
            _remoteObject.Request("Init");

            List<string[]> records = CSVEditor.Open("MarketTimeData.txt");
            foreach (string[] record in records)
            {
                DateTime time = ALib.DateTimeConverter.ToDateTime(record[0]);
                TimeData timeData = new TimeData();
                for (int i = 1; i < record.Length; i++)
                {
                    string[] tickerData = record[i].Split(new char[] { '|' });
                    timeData.Add(tickerData[0].Replace("_", ",")
                        , tickerData[1] == string.Empty ? double.NaN : double.Parse(tickerData[1])// Open
                                 , tickerData[2] == string.Empty ? double.NaN : double.Parse(tickerData[2])// High
                                 , tickerData[3] == string.Empty ? double.NaN : double.Parse(tickerData[3])// Low
                                 , tickerData[4] == string.Empty ? double.NaN : double.Parse(tickerData[4])// Close
                                 , tickerData[5] == string.Empty ? double.NaN : double.Parse(tickerData[5])// Volume
                                 , 0// existingOpenPosition
                                 , double.Parse(tickerData[6])// sessionTimePercentageLeft
                                 , double.Parse(tickerData[7])// Exchange rate
                                 , double.Parse(tickerData[8])// Point value
                                 , double.Parse(tickerData[9])// Min move
                                 , double.Parse(tickerData[10])// Lot size
                                 , double.Parse(tickerData[11])// CommissionOnRate
                                 , double.Parse(tickerData[12])// Commission per share in XXX
                                 , double.Parse(tickerData[13])// Min commission in XXX
                                 , double.Parse(tickerData[14])// Max commission in XXX
                                 , double.Parse(tickerData[15])// Stamp duty and slippage rate
                                 , double.Parse(tickerData[16])// Slippage per share in XXX
                        );
                    GoodValue.Assert(tickerData.Length == 17);
                }
                string calcCommand = timeData.ToCalcCommand(time);
                string result = _remoteObject.Request(calcCommand);

                Logger.WriteLine("Ouput.txt", result);
            }

            // Save log on server.
            _remoteObject.Request("SaveLog");

            // Save log on client.
            Logger.Save();
            Logger.LogDir = new DirectoryInfo(@"Log\" + DateTime.Now.ToString(DateTimeFormatType.Simple));
        }

        public static string GetCalcCommandForNic(string[] record)
        {
            DateTime time = ALib.DateTimeConverter.ToDateTime(record[0]);
            TimeData timeData = new TimeData();
            for (int i = 1; i < record.Length; i++)
            {
                string[] tickerData = record[i].Split(new char[] { '|' });
                timeData.Add(tickerData[0].Replace("_", ",")
                             , tickerData[1] == string.Empty ? double.NaN : double.Parse(tickerData[1]) // Open
                             , tickerData[2] == string.Empty ? double.NaN : double.Parse(tickerData[2]) // High
                             , tickerData[3] == string.Empty ? double.NaN : double.Parse(tickerData[3]) // Low
                             , tickerData[4] == string.Empty ? double.NaN : double.Parse(tickerData[4]) // Close
                             , tickerData[5] == string.Empty ? double.NaN : double.Parse(tickerData[5]) // Volume
                             , double.Parse(tickerData[6]) // existingOpenPosition
                             , double.Parse(tickerData[6 + 1]) // sessionTimePercentageLeft
                             , double.Parse(tickerData[7 + 1]) // Exchange rate
                             , double.Parse(tickerData[8 + 1]) // Point value
                             , double.Parse(tickerData[9 + 1]) // Min move
                             , double.Parse(tickerData[10 + 1]) // Lot size
                             , double.Parse(tickerData[11 + 1]) // CommissionOnRate
                             , double.Parse(tickerData[12 + 1]) // Commission per share in XXX
                             , double.Parse(tickerData[13 + 1]) // Min commission in XXX
                             , double.Parse(tickerData[14 + 1]) // Max commission in XXX
                             , double.Parse(tickerData[15 + 1]) // Stamp duty and slippage rate
                             , double.Parse(tickerData[16 + 1]) // Slippage per share in XXX
                    );
                GoodValue.Assert(tickerData.Length == 18);
            }
            string calcCommand = timeData.ToCalcCommand(time);
            return calcCommand;
        }
    }
}